package com.syc.jdbc.transaction_04;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import com.syc.jdbc.JdbcUtil;

public class DbTransaction {

	@SuppressWarnings("resource")
	@Test
	public void transactionTest() {

		Connection conn = null;
		PreparedStatement stmt = null;
		try {
			conn = JdbcUtil.getConnection();
			String sql_sub = "update acount set money=money-1000 where username=?";
			String sql_add = "update1 acount set money=money+1000 where username=?";

			//Connection默认就已经开启了事务.
			//是否自动提交事务----->事务的提交相当于事物的关闭
			//1.设置事务自动提交关闭
			conn.setAutoCommit(false);
			
			// 第一次执行:执行减钱的操作
			stmt = conn.prepareStatement(sql_sub);
			stmt.setString(1, "三胖");
			stmt.executeUpdate();

			// 第二次执行:执行加钱的操作
			stmt = conn.prepareStatement(sql_add);
			stmt.setString(1, "四呆");
			stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			//2.事务的回滚
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			JdbcUtil.close(stmt, conn);
			//3.手动提交事务.
			try {
				conn.commit();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
